flowchart TD
subgraph S1 [后端应用]
A[业务逻辑层]:::primary --> B[数据访问层 DAO]:::success
B --> C[JavaBean 实体对象]:::warning
end
subgraph S2 [数据库访问]
D[JDBC 驱动]:::info
E[数据库连接]:::info
F[SQL 执行]:::info
G[结果集处理]:::info
end
subgraph S3 [数据库]
H[关系型数据库]
end
%% 数据流向 - 查询操作
B -->|o1. 调用查询方法| D
D -->|o2. 建立连接| E
E -->|o3. 执行查询SQL| F
F -->|o4. 执行语句| H
H -->|o5. 返回结果集| G
G -->|o6. 封装结果| C
C -->|o7. 返回JavaBean对象| B
B -->|o8. 返回数据| A
%% 数据流向 - 增删改操作
A -->|i1. 传递JavaBean| B
B -->|i2. 提取属性值| C
C -->|i3. 生成SQL参数| D
D -->|i4. 建立连接| E
E -->|i5. 执行修改SQL| F
F -->|i6. 执行语句| H
H -->|i7. 返回操作结果| B
B -->|i8. 返回操作状态| A
class S3 warning
packagexust.demo.stu.dao;importjava.sql.*;importjava.util.ArrayList;importxust.stu.Result;importxust.stu.ConnectionUtil;importxust.demo.stu.domain.Student;publicclassStudentDaoImplimplementsStudentDao{publicResultinit(){Resultres=newResult(false);Connectioncon=null;Statementstmt=null;Stringsql="create table Student("+"no TEXT primary key,"+"name TEXT not null,"+"gender TEXT,"+"age INTEGER,"+"dept TEXT"+");";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.createStatement();ResultSetrs=stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'","Student".toUpperCase()));while(rs.next()){intcount=rs.getInt(1);if(count==0&&stmt.executeUpdate(sql)>0){res.code=0;}}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closeStmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}/** * 对象填充到关系 */privatevoidtoR(Studento,PreparedStatementstmt)throwsException{if(o!=null&&stmt!=null){Stringno=o.getNo();if(no!=null){stmt.setString(1,no);}Stringname=o.getName();if(name!=null){stmt.setString(2,name);}Stringgender=o.getGender();if(gender!=null){stmt.setString(3,gender);}Integerage=o.getAge();if(age!=null){stmt.setInt(4,age);}Stringdept=o.getDept();if(dept!=null){stmt.setString(5,dept);}}}/** * 关系填充到对象 */privateStudenttoO(ResultSetrs)throwsException{Studentres=null;if(rs!=null){res=newStudent();res.setNo(rs.getString("no"));res.setName(rs.getString("name"));res.setGender(rs.getString("gender"));res.setAge(rs.getInt("age"));res.setDept(rs.getString("dept"));}returnres;}publicResultcreate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="insert into Student values (?, ?, ?, ?, ?)";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultdelete(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="delete from Student where no = ?;";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);res.code=0;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultupdate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);stmt.setString(6,o.getNo());if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultget(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student where no = ?";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);ResultSetrs=stmt.executeQuery();Studentlast=null;while(rs.next()){last=newStudent();last.setNo(rs.getString("no"));last.setName(rs.getString("name"));last.setGender(rs.getString("gender"));last.setAge(rs.getInt("age"));last.setDept(rs.getString("dept"));}res.code=0;res.data=last;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultgetAll(){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);ResultSetrs=stmt.executeQuery();ArrayList<Student>data=newArrayList<Student>();while(rs.next()){Studentlast=toO(rs);data.add(last);}res.code=0;res.data=data;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}}
flowchart TD
subgraph 前端
A[用户界面]:::info
B[表单/AJAX请求]:::primary
C[数据展示/渲染]:::info
end
subgraph 后端
D[控制器/Servlet]:::warning
E[JavaBean 实体对象]:::danger
F[业务逻辑处理]:::info
end
subgraph 数据传递
G[HTTP请求/响应]:::success
H[参数解析]:::info
I[数据序列化/反序列化]:::info
end
%% 数据流向 - 前端到后端
A -->|i1. 输入数据| B
B -->|i2. 提交请求| G
G -->|i3. 接收请求| D
D -->|i4. 解析参数| H
H -->|i5. 封装数据| E
E -->|i8. 传递给业务逻辑| F
F -->|o1. 处理业务| E
E -->|o2. 封装结果| D
D -->|o3. 生成响应| G
G -->|o4. 接收响应| C
C -->|o5. 展示数据| A
%% JSP动作标签流程
D -->|i6. 使用useBean| E
D -->|i7. 使用setProperty| E
E -->|o6. 使用getProperty| C
flowchart TD
A[收到请求]:::success --> B{在scope中找到bean对象?}:::warning
B -- no --> C{class可被实例化?}:::warning
C -- no --> D[异常]:::danger
C -- yes --> E[id引用class对象]:::success
E --> G[处理jsp:useBean元素内容]:::success
B -- yes --> F{有无type?}:::warning
F -- yes --> H[类型转换]:::info
H --> I{转换成功?}:::warning
I -- yes --> G
I -- no --> J[异常]:::danger
F -- no --> G
G --> K[................]:::info
packagexust.demo.stu.dao;importjava.sql.*;importjava.util.ArrayList;importxust.stu.Result;importxust.stu.ConnectionUtil;importxust.demo.stu.domain.Student;publicclassStudentDaoImplimplementsStudentDao{publicResultinit(){Resultres=newResult(false);Connectioncon=null;Statementstmt=null;Stringsql="create table Student("+"no TEXT primary key,"+"name TEXT not null,"+"gender TEXT,"+"age INTEGER,"+"dept TEXT"+");";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.createStatement();ResultSetrs=stmt.executeQuery(String.format("select count(*) from sqlite_master where type = 'table' and Upper(name) = '%s'","Student".toUpperCase()));while(rs.next()){intcount=rs.getInt(1);if(count==0&&stmt.executeUpdate(sql)>0){res.code=0;}}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closeStmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}/** * 对象填充到关系 */privatevoidtoR(Studento,PreparedStatementstmt)throwsException{if(o!=null&&stmt!=null){Stringno=o.getNo();if(no!=null){stmt.setString(1,no);}Stringname=o.getName();if(name!=null){stmt.setString(2,name);}Stringgender=o.getGender();if(gender!=null){stmt.setString(3,gender);}Integerage=o.getAge();if(age!=null){stmt.setInt(4,age);}Stringdept=o.getDept();if(dept!=null){stmt.setString(5,dept);}}}/** * 关系填充到对象 */privateStudenttoO(ResultSetrs)throwsException{Studentres=null;if(rs!=null){res=newStudent();res.setNo(rs.getString("no"));res.setName(rs.getString("name"));res.setGender(rs.getString("gender"));res.setAge(rs.getInt("age"));res.setDept(rs.getString("dept"));}returnres;}publicResultcreate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="insert into Student values (?, ?, ?, ?, ?)";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultdelete(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="delete from Student where no = ?;";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);res.code=0;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultupdate(Studento){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?";con=ConnectionUtil.getConnection();if(o!=null&&con!=null){try{stmt=con.prepareStatement(sql);toR(o,stmt);stmt.setString(6,o.getNo());if(stmt.executeUpdate()>0){res.code=0;}}catch(Exceptione){e.printStackTrace();res.message=e.getMessage();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultget(Stringno){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student where no = ?";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);stmt.setString(1,no);ResultSetrs=stmt.executeQuery();Studentlast=null;while(rs.next()){last=newStudent();last.setNo(rs.getString("no"));last.setName(rs.getString("name"));last.setGender(rs.getString("gender"));last.setAge(rs.getInt("age"));last.setDept(rs.getString("dept"));}res.code=0;res.data=last;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}publicResultgetAll(){Resultres=newResult(false);Connectioncon=null;PreparedStatementstmt=null;Stringsql="select * from Student";con=ConnectionUtil.getConnection();if(con!=null){try{stmt=con.prepareStatement(sql);ResultSetrs=stmt.executeQuery();ArrayList<Student>data=newArrayList<Student>();while(rs.next()){Studentlast=toO(rs);data.add(last);}res.code=0;res.data=data;}catch(Exceptione){res.message=e.getMessage();e.printStackTrace();}finally{ConnectionUtil.closePstmt(stmt);ConnectionUtil.closeConnection(con);}}returnres;}}